package demo1;

public class Sort {
    //1.直接插入排序
    public static void insertSort(int[] array) {
        for(int i = 1 ;i < array.length ;i++) {
            int temp = array[i];
            int j = i - 1;
            for(;j >= 0 ;j--) {
                if(temp < array[j]) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = temp;
        }
    }
    //2.希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length/2;
        while(gap > 0) {
            for(int i = gap; i < array.length; i++) {
                int temp = array[i];
                int j = i - gap;
                for(;j >= 0 ; j--) {
                    if(temp < array[j]) {
                        array[j+gap] = array[j];
                    } else {
                        break;
                    }
                }
                array[j+gap] = temp;
            }
            gap = gap/2;
        }
    }
    //3.直接选择排序
    public static void selectSort(int[] array) {
        for(int i = 0 ; i < array.length ; i++) {
            int minIndex = i;
            for(int j = i + 1 ; j < array.length ; j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }
    //4.冒泡排序
    public static void bubbleSort(int[] array) {
        for(int i = 0 ; i < array.length ; i++) {
            boolean flag = false;
            for(int j = 0 ; j < array.length-1-i ; j++) {
                if (array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flag = true;
                }
            }
            if(flag == false) {
                break;
            }
        }
    }
    //5，堆排序
    public static void heapSort(int[] array) {
        creatBigHeap(array);
        int end = array.length-1;
        while(end > 0) {
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }

    private static void creatBigHeap(int[] array) {
        for(int i = (array.length-1-1)/2 ; i >=0 ; i++) {
            shiftDown(array,i,array.length);
        }
    }

    private static void shiftDown(int[] array, int parent, int end) {
        int child = 2*parent + 1;
        while(child < end) {
            if(child + 1 < end && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2*parent + 1;
            } else {
                break;
            }
        }
    }
    //6.快速排序
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int left, int right) {
        if(left >= right) {
            return;
        }
        int pivot = parttion(array,left,right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }

    private static int parttion(int[] array, int left, int right) {
        int pivot = array[left];
        while(left < right) {
            while(left < right && array[right] > pivot) {
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] < pivot) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = pivot;
        return left;
    }
    //7.归并排序
    public static void mergeSort(int[] array) {
        mergeSortFuc(array,0,array.length-1);
    }

    private static void mergeSortFuc(int[] array, int left, int right) {
        if(left >= right) {
            return;
        }
        int mid = (left + right)/2;
        mergeSortFuc(array,left,mid);
        mergeSortFuc(array,mid+1,right);
        merge(array,left,mid,right);
    }

    private static void merge(int[] array, int left, int mid, int right) {
        int s1 = left;
        int s2 = mid + 1;
        int e1 = mid;
        int e2 = right;
        int[] temp = new int[right-left+1];
        int k = 0;
        while(s1 <= mid && s2 <= right) {
            if(array[s1] <= array[s2]) {
                temp[k++] = array[s1++];
            } else {
                temp[k++] = array[s2++];
            }
        }
        while(s1 <= mid) {
            temp[k++] = array[s1++];
        }
        while(s2 <= right) {
            temp[k++] = array[s2++];
        }
        for(int i = 0 ; i < temp.length ; i++) {
            array[left+i] = temp[i];
        }
    }

    private static void swap(int[] array, int i,int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
